/**
 * Copyright  2011 Nokia Corporation.
 *
 */

#include "TileInterfaces.h"
#include "ParticleEngine.h"


/**
 *
 * Constructor
 *
 */
ITileGame::ITileGame( ITileRenderer *renderer, int x, int y, int width, int height )
{
    m_renderer = renderer;
    m_pengine = new CParticleEngine( renderer );
    m_logoState = 0;
    m_hudState = 0;
    m_state = eTILEGAMESTATE_NOTSET;
    setGameArea( x,y,width,height );
}


/**
 *
 *
 *
 */
ITileGame::~ITileGame()
{
    if (m_pengine) {
        delete m_pengine;
    };
}

/**
 *
 * a Click is arriving from the framework
 *
 */
void ITileGame::click( int fixedx, int fixedy, eMOUSEEVENT etype )
{
    // TODO, transfer basic functionality here.
    gameClick( fixedx, fixedy, etype );
}


/**
 *
 * Change the game state. Send notifications accordingly
 *
 */
void ITileGame::setGameState( eTILEGAMESTATE state )
{
    switch ( state ) {
    case eTILEGAMESTATE_MENU:
        m_renderer->effectNotify( eEFFECT_MENU, 0, 0);
        break;
    case eTILEGAMESTATE_GAMEOVER:
        m_renderer->effectNotify( eEFFECT_GAMEOVER, 0, 0);
        break;
    }
    m_state = state;

    gameStateChanged();			// notify
}


/**
 *
 * Run the TileGame
 *
 */
int ITileGame::run( int fixedFrameTime16Bit )
{
    int hudStateTarget = 0;
    int logoStateTarget = 0;

    switch ( m_state ) {
    case eTILEGAMESTATE_NOTSET:
        if (load() == 1) {
            setGameState( eTILEGAMESTATE_PAUSED );
        } else setGameState( eTILEGAMESTATE_MENU );
        break;

    case eTILEGAMESTATE_MENU:
        logoStateTarget = 65535;
        break;

    case eTILEGAMESTATE_RUNGAME:
    {
        hudStateTarget = 65535;
    };
        break;

    };

    m_logoState += (((logoStateTarget-m_logoState) * fixedFrameTime16Bit) >> 13);
    m_hudState += (((hudStateTarget-m_hudState) * fixedFrameTime16Bit) >> 14);

    int grun = gameRun( fixedFrameTime16Bit );
    m_pengine->run( fixedFrameTime16Bit);

    return 1;
}

/**
 *
 * Draw the tilegame
 *
 */
int ITileGame::draw()
{
    m_renderer->renderBackground(0);			// game will render bg
    int rval = gameDraw();
    m_renderer->renderForeground(0);
    return rval;
}



